#include<stdio.h>
#include<math.h>
#include<graphics.h>
#include<mmststem.h>
#include <pthread.h>
#pragma comment(lib,"winmm.lib")
#define WIN_WIDTH 1024; // 窗口的大小
#define WIN_HEIGHT 640;
#define MAP_WIDTH(WIN_WIDTH*3) // 地图的大小
#define MAP_HEIGHT(WIN_HEIGHT*3)
#define FOOD_NUM 1000
#define AI_NUM 200
// 地图是一张图 int temp;
IMAGE map(MAP_WIDTH, MAP_HEIGHT);

//食物，玩家，ai有什么属性
struct Ball {
    int x;
    int y;
    int r;
    DWORD color; // 颜色
    bool flag; // 是否存在
};

struct Ball food[FOOD_NUM];
struct Ball player;
struct Ball ai[AI_NUM];
POINT g_CameraPos; // 定义摄像机位置
void ChaseAlgortihm(struct Ball *chase, struct Ball run);

// 求两个球之间的距离
double Distance(struct Ball b1, struct Ball b2) {
    return sqrt((double) (b1.x - b2.x) * (b1.x - b2.x) + (b1.y - b2.y) * (b1.y - b2.y));
}

void updatePos() {
    g_CameraPos.x = player.x - WIN_WIDTH
    /
    2;
    g_CameraPos.y = player.y - WIN_HEIGHT
    /
    2;
    //防止越界
    if (g_CameraPos.x < 0)
        g_CameraPos.x = 0;
    if (g_CameraPos.y < 0)
        g_CameraPos.y = 0;
    if (g_CameraPos.x > MAP_WIDTH - WIN_WIDTH)
        g_CameraPos.x = MAP_WIDTH - WIN_WIDTH;
    if (g_CameraPos.y > MAP_HEIGHT - WIN_HEIGHT)
        g_CameraPos.y = MAP_HEIGHT - WIN_HEIGHT;
}

// 初始化数据
void GameInit() {
    // 设置随机数种子
    srand(GetTickCount());
    // 初始化食物
    for (int i = 0; i < FOOD_NUM; i++) {
        // rand() 随机生成一个整数
        food[i].x = rand() % MAP_WIDTH;
        food[i].y = rand() % MAP_HEIGHT;
        food[i].r = rand() % 5 + 1;
        food[i].flag = true;
        food[i].color = RGB(rand() % 256, rand() % 256, rand() % 256);
    }
    // 初始化玩家
    player.x = rand() % MAP_WIDTH;
    player.y = rand() % MAP_HEIGHT;
    player.flag = true;
    player.color = RGB(rand() % 256, rand() % 256, rand() % 256);
    player.r = rand() % 10 + 10;
    // 初始化ai
    for (int i = 0; i < AI_NUM; i++) {
        ai[i].x = rand() % MAP_WIDTH;
        ai[i].y = rand() % MAP_HEIGHT;
        ai[i].flag = true;
        ai[i].r = rand() % 10 + 15;
        ai[i].color = RGB(rand() % 256, rand() % 256, rand() % 256);
    }
}

void GameDraw() {
    // 播放背景音乐，repeat重复播放，alias取别名
    mciSendString("open ./BallBGM.mp3 alias BGM", 0, 0, 0);
    mciSendString("play BGM repeat,0,0,0");

    // 设置工作区
    SetWorkingImage(&map);
    setbkcolor(WHITE); // 设置背景颜色
    cleardevice(); // 初始化窗口
    // 绘制食物
    for (int i = 0; i < FOOD_NUM; i++) {
        if (FOOD[i].flag) {
            setfillcolor(food[i].color);
            fillcircle(food[i].x, food[i].y, food[i].r);
        }
    }
    // 绘制ai
    for (int i = 0; i < AI_NUM; i++) {
        if (ai[i].flag) {
            setfillcolor(ai[i].color);
            fillcircle(ai[i].x, ai[i].y, ai[i].r);
        }
    }
    // 绘制玩家
    setfillcolor(player.color);
    solidcircle(player.x, player.y, player.r);

    SetWorkingImage();
    updatePos(); // 更新摄像机位置
    // 把自定义的图片绘制到窗口
    putimage(0, 0,WIN_WIDTH
    ,
    WIN_HEIGHT
    ,
    &map, g_CameraPos.x, g_CameraPos.y
    )
    ;
}

// 移动起来了，飘起来了
void PlayerMove(int speed) {
    // 获取键盘输入 _getch();
    if (GetAsyncKeyState(VK_UP)) {
        player.y -= speed;
    }
    if (GetAsyncKeyState(VK_DOWN)) {
        player.y += speed;
    }
    if (GetAsyncKeyState(VK_LEFT)) {
        player.x -= speed;
    }
    if (GetAsyncKeyState(VK_RIGHT)) {
        player.x += speed;
    }
    // 作弊测试

    if (GetAsyncKeyState(VK_SPACE)) {
        player.r++;
    }
    if (GetAsyncKeyState('A')) {
        player.r--;
    }
}

// 吃零食
void EatFood() {
    for (int i = 0; i < FOOD_NUM; i++) {
        if (food[i].flag && Distance(player, food[i]) < player.r) {
            // 如果能吃
            food[i].flag = false;
            player.r += food[i].r / 4;
        }
    }
}

//AI移动算法，追逐比自己半径小的球
void AiMove() {
    // 找到每一个人工智障
    for (int i = 0; i < AI_NUM; i++) {
        // 设置搜索范围
        int min_DISTANCE = MAP_WIDTH;
        int index = -1;
        if (ai[i].flag) {
            // 每一个都和后面的比较一下
            for (int k = i + 1; k < AI_NUM; ++k) {
                if (ai[i].r > ai[k].r && ai[k].flag && Distance(ai[i], ai[k]) < min_DISTANCE) {
                    min_DISTANCE = Distance(ai[i], ai[k]);
                    index = k;
                }
            }
        }
        // 去追吧
        if (index == -1) {
            ChaseAlgortihm(&ai[i], ai[index]);
        } else {
            ChaseAlgortihm(&ai[i], food[rand() % FOOD_NUM]);
        }
    }
}

// 玩家吃AI,AI吃玩家
void EatAI() {
}

// AI吃食物，AI吃ai
void AiEatFood() {
}


int main() {
    // 1. 创建一个图形窗口
    initgraph(WIN_WIDTH
    ,
    WIN_HEIGHT
    )
    ;
    GameInit();
    // 游戏，要不断地去处理，所以说要循序
    while (1) {
        GameDraw();
        PlayerMove(5);
        EatFood();
        AiMove();
    }
    getchar(); // 防止程序闪退

    return 0;
}

// 追逐算法
void ChaseAlgortihm(struct Ball *chase, struct Ball run) {
    // 已知逃跑者的坐标，那么，chase向run移动是不是就可以了
    if (rand() % 2 == 0) {
        if (chase->x < run.x) {
            chase->x += 2;
        } else {
            chase->x -= 2;
        }
    } else {
        if (chase->y < run.y) {
            chase->y += 2;
        } else {
            chase->y -= 2;
        }
    }
}
